package com.peak.distribution.entity.order;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.LazyToOne;
import org.hibernate.annotations.LazyToOneOption;

import com.peak.distribution.entity.agent.DisAgent;
import com.peak.distribution.entity.goods.DisGoods;
import com.peak.distribution.enums.Constants;
import com.peak.spring.boot.api.entity.DefaultStategyEntity;

import lombok.Data;
import lombok.EqualsAndHashCode;

@Entity
@Table(name = "dis_cart")
@Data
@EqualsAndHashCode(callSuper=true)
public class DisCart extends DefaultStategyEntity {

	private static final long serialVersionUID = Constants.DISTRIBUTION_VERSION;

	/** 购买数量 */
	private Integer quantity;

	/** 购买人 */
	private DisAgent agent;

	/** 商品 */
	private DisGoods goods;

	/** 商品加入购物车时间 */
	private Date createTime;

	public int addQuantity(int quantity) {
		if (this.quantity == null) {
			this.quantity = 0;
		}
		return this.quantity += quantity;
	}

	@ManyToOne
	@JoinColumn(name = "agent_id")
	@Fetch(FetchMode.SELECT)
	@LazyToOne(LazyToOneOption.PROXY)
	public DisAgent getAgent() {
		return agent;
	}

	@ManyToOne
	@JoinColumn(name = "goods_id")
	@Fetch(FetchMode.SELECT)
	@LazyToOne(LazyToOneOption.PROXY)
	public DisGoods getGoods() {
		return goods;
	}
}